package com.anti.modular.bus.housebill.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.commom.api.Result;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.bus.housebill.entity.Cty;
import com.anti.modular.bus.housebill.entity.HouseBillExcel;
import com.anti.modular.bus.housebill.param.HouseBillDtParam;
import com.anti.modular.bus.housebill.param.HouseBillParam;
import com.anti.modular.bus.housebill.service.HouseBillService;
import com.anti.modular.commonutils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 账单控制器
 *
 * @author anti
 * @date 2021-05-07 09:59:46
 */
@RestController
@RequestMapping("houseBill")
public class HouseBillController {

    @Resource
    private HouseBillService houseBillService;

    /**
     * 查询账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @GetMapping("page")
    public Result page(PageParam pageParam, HouseBillParam houseBillParam) {
        return Result.data(houseBillService.page(pageParam,houseBillParam));
    }

    /**
     * 主页账单
     *
     */
    @GetMapping("analysispage")
    public Result analysispage(PageParam pageParam, HouseBillParam houseBillParam) {
        return Result.data(houseBillService.analysispage(pageParam,houseBillParam));
    }

    @GetMapping("pageByLeaseId")
    public Result houseBillPageByLeaseId(PageParam pageParam, HouseBillParam houseBillParam) {
        return Result.data(houseBillService.houseBillPageByLeaseId(pageParam,houseBillParam));
    }

    /**
     * 添加账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @PostMapping("add")
    @BusinessLog(title = "账单_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(HouseBillParam.add.class) HouseBillParam houseBillParam) {
            houseBillService.add(houseBillParam);
        return Result.success("新增成功");
    }

    /**
     * 删除账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @PostMapping("delete")
    @BusinessLog(title = "账单_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(HouseBillParam.delete.class) HouseBillParam houseBillParam) {
            houseBillService.delete(houseBillParam);
        return Result.success("删除成功");
    }

    /**
     * 编辑账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @PostMapping("edit")
    @BusinessLog(title = "账单_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(HouseBillParam.edit.class) HouseBillParam houseBillParam) {
            houseBillService.edit(houseBillParam);
        return Result.success("修改成功");
    }
    /**
     * 编辑账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @PostMapping("editDt")
    @BusinessLog(title = "账单_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editDt(@RequestBody @Validated(HouseBillDtParam.edit.class) HouseBillDtParam houseBillParam) {
        houseBillService.editDt(houseBillParam);
        return Result.success("修改成功");
    }
    /**
     * 编辑账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @PostMapping("editTuoQian")
    @BusinessLog(title = "账单_编辑拖欠", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editTuoQian(@RequestBody HouseBillParam houseBillParam) {
        houseBillService.editTuoQian(houseBillParam);
        return Result.success("修改成功");
    }

    @PostMapping("editDaozang")
    @BusinessLog(title = "账单_到賬", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editDaozang(@RequestBody HouseBillParam houseBillParam) {
        houseBillService.editDaozang(houseBillParam);
        return Result.success("修改成功");
    }

    @PostMapping("editCheXiaoDaoZang")
    @BusinessLog(title = "账单_撤銷到賬", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editCheXiaoDaoZang(@RequestBody HouseBillParam houseBillParam) {
        houseBillService.editCheXiaoDaoZang(houseBillParam);
        return Result.success("修改成功");
    }


    @PostMapping("editQingSuan")
    @BusinessLog(title = "账单_清算到賬", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result editQingSuan(@RequestBody HouseBillParam houseBillParam) {
        houseBillService.editQingSuan(houseBillParam);
        return Result.success("清算成功");
    }

    /**
     * 查看账单
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @GetMapping("detail")
    public Result detail(@Validated(HouseBillParam.detail.class) HouseBillParam houseBillParam) {
        return Result.data(houseBillService.detail(houseBillParam));
    }

    /**
     * 账单列表
     *
     * @author anti
     * @date 2021-05-07 09:59:46
     */
    @GetMapping("list")
    public Result list(HouseBillParam houseBillParam) {
        return Result.data(houseBillService.list(houseBillParam));
    }


    @GetMapping("getPrintMsgForBill/{id}")
    public Result getPrintMsgForBill(@PathVariable(name="id") Long id) {
        return Result.data(houseBillService.getPrintMsgForBill(id));
    }


    /**
     * 获取账单计划列表
     *
     * @author anti
     * @date 2021-05-20 14:25:38
     */
    @GetMapping("getBillListByLeaseId/{leaseId}")
    public Result getBillListByLeaseId(@PathVariable(name = "leaseId") Long leaseId) {
        return Result.data(houseBillService.getBillListByLeaseId(leaseId));
    }

    @GetMapping("getWaterBillById/{id}")
    public Result getWaterBillById(@PathVariable(name = "id") Long id) {
        return Result.data(houseBillService.getWaterBillById(id));
    }

    @PostMapping("getHouseBillNum")
    public Result getHouseBillNum(@RequestBody HouseBillParam houseBillParam){
        return Result.data(houseBillService.houseBillService(houseBillParam));
    }
    @PostMapping("exportExcel")
    @BusinessLog(title = "账单_導出EXCEL", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void exportExcel(@RequestBody HouseBillParam houseBillParam, HttpServletResponse response){
        int excelType = houseBillParam.getExcelType();
        List<HouseBillExcel> exportData = houseBillService.getExportData(houseBillParam);
        String title="已收賬單";
        String fileName="已收賬單"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        if (excelType==1){
            title="未收賬單";
            fileName="未收賬單"+ DateUtil.format(new Date(),"yyyy-MM-dd");
        }
        try {
            ExcelUtils.exportExcel(exportData,title,title, HouseBillExcel.class, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
